//
//  NetworkHandle.m
//  UI_新版豆瓣
//
//  Created by Scott on 15/7/31.
//  Copyright (c) 2015年 Scott. All rights reserved.
//

#import "NetworkHandle.h"

@implementation NetworkHandle

+ (void)POSTWithURL:(NSString *)urlStr httpBody:(NSString *)httpbody delegate:(id<NetworkHandleDelegate>)delegate
{
    /* 转码 */
    NSString *str = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:str]];
    
    request.HTTPMethod = @"POST";
    request.HTTPBody = [httpbody dataUsingEncoding:NSUTF8StringEncoding];
    
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        /* JSON Parser */
        NSError *error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

        /* 向代理人发送消息, 执行协议方法 */
        [delegate didFinishLoadData:result];
    }];
}





+ (void)GETWithURL:(NSString *)urlStr delegate:(id<NetworkHandleDelegate>)delegate
{
    /* 转码 */
    NSString *str = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    /* 创建URL请求*/
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:str]];
    
    /* 异步连接 */
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        /* JSON 解析 */
        NSError *error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        
        /* 向代理人发送消息,执行协议方法 */
        [delegate didFinishLoadData:result];
        
    }];
}





/* POST 异步 */
+ (void)POSTWithURL:(NSString *)urlStr httpBody:(NSString *)httpBody completion:(void (^)(id))block
{
    /* 转码 */
    NSString *str = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:str]];
    
    request.HTTPMethod = @"POST";
    request.HTTPBody = [httpBody dataUsingEncoding:NSUTF8StringEncoding];
    
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
       
        /* JSON Parser */
        NSError *error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        
        /* block 回调 */
        block(result);
        
    }];
}

/* GET异步 */
+ (void)GETWithURL:(NSString *)urlStr completion:(void (^)(id))block
{
    /* 转码 */
    NSString *str = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    /* 创建URL请求*/
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:str]];
    
    
    /* 异步连接 */
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
       /* JSON 解析 */
        NSError *error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        
        /* block 回调数据 */
        block(result);
    
    }];
    
}

@end
